# DjangoDE, an integrated development environment for Django
# Copyright (C) 2010-2011 Andrew Wilkinson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

from djangode import global_objects
from djangode.utils import get_config_value, set_config_value
from djangode.utils.qt import QtCore, QtGui

class DjangoDESettingsDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)

        self.setWindowTitle("Project Settings")
        self.setWindowIcon(QtGui.QIcon(global_objects.icon))

        layout = QtGui.QGridLayout(self)

        layout.addWidget(QtGui.QLabel("Comment"), 0, 0)
        self.comment_colour_button = QtGui.QPushButton("")
        self.comment_colour = get_config_value("comment_colour")
        self.comment_colour_button.setStyleSheet(" * { background-color: rgb(%i, %i, %i); }" % self.comment_colour)
        self.comment_colour_button.clicked.connect(self.choose_colour("comment_colour"))
        layout.addWidget(self.comment_colour_button, 0, 1)


        layout.addWidget(QtGui.QLabel("Keyword"), 1, 0)
        self.keyword_colour_button = QtGui.QPushButton("")
        self.keyword_colour = get_config_value("keyword_colour")
        self.keyword_colour_button.setStyleSheet(" * { background-color: rgb(%i, %i, %i); }" % self.keyword_colour)
        self.keyword_colour_button.clicked.connect(self.choose_colour("keyword_colour"))
        layout.addWidget(self.keyword_colour_button, 1, 1)

        layout.addWidget(QtGui.QLabel("String"), 2, 0)
        self.string_colour_button = QtGui.QPushButton("")
        self.string_colour = get_config_value("string_colour")
        self.string_colour_button.setStyleSheet(" * { background-color: rgb(%i, %i, %i); }" % self.string_colour)
        self.string_colour_button.clicked.connect(self.choose_colour("string_colour"))
        layout.addWidget(self.string_colour_button, 2, 1)

        self.close_button = QtGui.QPushButton("Close")
        self.close_button.setDefault(True)
        self.close_button.clicked.connect(self.close)
        layout.addWidget(self.close_button, 3, 1, QtCore.Qt.AlignRight)

    def closeEvent(self, event):
        set_config_value("comment_colour", self.comment_colour)
        set_config_value("keyword_colour", self.keyword_colour)
        set_config_value("string_colour", self.string_colour)

        return QtGui.QDialog.closeEvent(self, event)

    def choose_colour(self, colour_name):
        def colour_picker():
            picked = QtGui.QColorDialog.getColor(QtGui.QColor(*getattr(self, colour_name)), self)

            setattr(self, colour_name, (picked.red(), picked.green(), picked.blue()))
            getattr(self, colour_name + "_button").setStyleSheet(" * { background-color: rgb(%i, %i, %i); }" % getattr(self, colour_name))
        return colour_picker
